package com.ftpserver;

import java.nio.ByteBuffer;

public class TestBuffer {
    public static void main(String[] args) {
        ByteBuffer byteBuffer=ByteBuffer.allocate(20);
        byteBuffer.put("hello".getBytes());
        byteBuffer.flip();
        ByteBuffer nowByteBuffer=ByteBuffer.allocate(20);
        nowByteBuffer.put(0,byteBuffer,0,5);
        nowByteBuffer.limit(5);
        nowByteBuffer.position(5);

        nowByteBuffer.flip();
        while(nowByteBuffer.hasRemaining()){
            System.out.println(nowByteBuffer.get());
        }
  /*
        // 创建两个 ByteBuffer 实例
        ByteBuffer sourceBuffer = ByteBuffer.allocate(10);
        ByteBuffer targetBuffer = ByteBuffer.allocate(20);

        sourceBuffer.put("hello".getBytes());

        // 重置 position 到 0 准备读取数据
        sourceBuffer.flip();

        // 将 sourceBuffer 中的部分数据复制到 targetBuffer 中
        targetBuffer.put(targetBuffer.position(), sourceBuffer, 0, 4); // 从 sourceBuffer 的索引 2 开始复制 4 个字节到 targetBuffer 的索引 5 处

        // 打印 targetBuffer 中的数据以验证结果
        System.out.println("Target Buffer Content:");
        targetBuffer.position(0); // 重置 position 以便读取数据
        while (targetBuffer.hasRemaining()) {
            System.out.print(targetBuffer.get() + " ");
        }
        */
    }
}
